﻿-- обновляет статистику таблиц
CREATE PROCEDURE [dbo].[USPTOOLS_DBSTATSUpdateStatsAllDB]
WITH EXECUTE AS CALLER
AS
create table #TempTbl (DBName varchar(50), Process int default 0)
insert into #TempTbl (DBName) select name from master..sysdatabases where dbid not in(1,2,3,4)
declare @sDbname varchar(50)
 while exists (select 'x' from #TempTbl where Process = 0)
begin
   select top 1 @sDbname = DBName from #TempTbl where Process = 0
   exec ('USE  ' +  '[' + @sDbname + ']  EXEC sp_updatestats') 
   update #TempTbl 
   set    Process = 1
   where  DBName = @sDbname   
end
drop table #TempTbl


